package tools;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Tools {


	public static void writeToFile(String filename,String value) throws IOException
	{
		//Write gnuplot file
		PrintWriter ecrivain =  new PrintWriter(new BufferedWriter(new FileWriter(filename)));
		ecrivain.println(value);
		ecrivain.close();
	}

	public static void createDirectory(String path){
		try {
			Runtime.getRuntime().exec("mkdir "+path);

			Thread.sleep(100L); // laisser le temps de la creation...
		} catch (IOException e) {

			e.printStackTrace();
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
	}

	public static ArrayList<Integer> sortDecreaseIndex(ArrayList<Double> scores, int trunc) {
		ArrayList<Pair<Integer,Double>> bufArr= new ArrayList<Pair<Integer,Double>>(scores.size());
		for(int i=0; i<scores.size(); i++){
			bufArr.add(new Pair<Integer,Double>(i,scores.get(i)));
		}

		Collections.sort(bufArr, new MonComparateur() );

		ArrayList<Integer> index = new ArrayList<Integer>();
		for(int i=bufArr.size()-1; i>=bufArr.size()-trunc; i--){
		//for(int i=0; i<trunc; i++){
			index.add(bufArr.get(i).getX());
			//System.out.print(bufArr.get(i).getY()+" ");
		}
		//System.out.println();
		return index;
	}

	

}


final class MonComparateur implements Comparator<Pair<Integer,Double>>{
	public int compare(Pair<Integer,Double> a, Pair<Integer,Double> b){
		if(a.getY()>b.getY())
			return 1;
		else if (a.getY()<b.getY())
			return -1;
		else return 0;
	}
}
